//
//  ZMyAskViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/3/28.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZMyAskViewController.h"
#import "ZWaitReplyViewController.h"
#import "ZAlreadyViewController.h"
#import "UIView+CFYExtension.h"
@interface ZMyAskViewController ()<UIScrollViewDelegate>
{
   
}
@property (nonatomic,weak)UIView *indicatorView;
//当前选中的按钮
@property (nonatomic,weak)UIButton *selectedBt;
//titleView
@property (nonatomic,weak)UIView *titleView;
//contentView
@property (nonatomic,weak)UIScrollView *contentView;
@property (nonatomic,assign) NSInteger index;
@end

@implementation ZMyAskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupChildVces];
    [self setTitleView];
    [self setupScrollView];
}

//添加子控制器
- (void)setupChildVces
{
    //待回复
    ZWaitReplyViewController * disVC = [[ZWaitReplyViewController alloc]init];
    disVC.title = @"待回复";
    [self addChildViewController:disVC];
    
    //已回复
    ZAlreadyViewController *caseVC = [[ZAlreadyViewController alloc]init];
    caseVC.title = @"已回复";
    [self addChildViewController:caseVC];
    
    
}
//按钮View
- (void)setTitleView
{
    CGFloat TitleViewH = 35;
    CGFloat TitleViewY = 0;
    UIView *titleView = [[UIView alloc]init];
    titleView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7];
    titleView.width = SCREEN_Width;
    titleView.height = TitleViewH;
    titleView.y = TitleViewY;
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //底部线
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = KHGreenColor;
    indicatorView.tag = -1;
    indicatorView.height = 2;
    indicatorView.y = titleView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    
    
    CGFloat width = titleView.width / self.childViewControllers.count;
    CGFloat height = titleView.height;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        UIButton *bt = [[UIButton alloc]init];
        bt.tag = i;
        bt.height = height;
        bt.width = width ;
        bt.x = i * width;
        [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [bt setTitleColor:KHGreenColor forState:UIControlStateDisabled];
        bt.titleLabel.font =[UIFont systemFontOfSize:14];
        UIViewController *vc = self.childViewControllers[i];
        [bt setTitle:vc.title forState:UIControlStateNormal];
        [bt addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:bt];
        
        //默认点击第一个按钮
        if (i == 0) {
            bt.enabled = NO;
            self.selectedBt = bt;
            [bt.titleLabel sizeToFit];
            self.indicatorView.width = bt.titleLabel.width;
            self.indicatorView.centerX = bt.centerX;
        }
    }
    [titleView addSubview:indicatorView];
    
}
//中间滑动视图
- (void)setupScrollView{
    //不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame  = CGRectMake(0, CGRectGetMaxY(self.titleView.frame), SCREEN_Width, SCREEN_Height-CGRectGetMaxY(self.titleView.frame)-108*ysIZE);
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(contentView.width *self.childViewControllers.count, 0);
    contentView.showsVerticalScrollIndicator = NO;
    contentView.showsHorizontalScrollIndicator = NO;
    [self.view insertSubview:contentView atIndex:0];
    self.contentView = contentView;
    [self scrollViewDidEndScrollingAnimation:contentView];
    
}
#pragma mark --ScrollViewDelegate
//监听动画完毕 titleView滚动完成后
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    NSInteger index =  scrollView.contentOffset.x/scrollView.width;
    
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.height = scrollView.height;
    
    [scrollView addSubview:vc.view];
}
//手势拖拽后 减速完毕
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index =scrollView.contentOffset.x/scrollView.width;
    if (self.index != index ) {
        if (_index == 0) {//待抢页面通知 写在这里 待抢订单右滑不用刷新
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh0" object:nil];
            
        }else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh1" object:nil];
            
        }
    }
    
    self.index = index;
    [self titleClick:self.titleView.subviews[index]];
}


#pragma mark --Button点击
- (void)titleClick:(UIButton *)button{
    self.selectedBt.enabled = YES;
    button.enabled = NO;
    self.selectedBt = button;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    //滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag *self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
